/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bblocks.common.NoRepeatSubmit;
import com.bblocks.util.R;
import com.pig4cloud.pig.admin.api.entity.SysArea;
import com.pig4cloud.pig.admin.api.entity.SysDict;
import com.pig4cloud.pig.admin.api.entity.SysDictItem;
import com.pig4cloud.pig.admin.service.SysAreaService;
import com.pig4cloud.pig.admin.service.SysDictItemService;
import com.pig4cloud.pig.admin.service.SysDictService;
import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.awt.geom.Area;
import java.util.List;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author lengleng
 * @since 2019-03-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/area")
@Tag(description = "area", name = "区域管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysAreaController {

	private final SysAreaService sysAreaService;


	/**
	 * 通过ID查询区域信息
	 * @param id ID
	 * @return 字典信息
	 */
	@Inner(false)
	@GetMapping("/details/{id}")
	public R<SysArea> getById(@PathVariable Long id) {
		return R.ok(sysAreaService.get(id));
	}


	/**
	 * 通过code查询区域信息
	 * @param code
	 * @return
	 */
	@Inner(false)
	@GetMapping("/detailsByCode/{code}")
	public R<SysArea> getByCode(@PathVariable String code) {
		return R.ok(sysAreaService.getByCode(code));
	}

	@Inner(false)
	@Operation(summary = "获取下级数据", description = ""
			,parameters = {
				@Parameter(name = "parentCode", description = "父code(为空则省份)",required = false),//好像不生效
			}
	)
	@GetMapping("/getByParentCode")
	public R<List<SysArea>> getByParentCode(String parentCode) {
		if(StrUtil.isEmpty(parentCode)){
			return R.ok(sysAreaService.getByLv(1));
		}else{
			return R.ok(sysAreaService.getByParentCode(parentCode));
		}
	}

	@NoRepeatSubmit(lockTime = 1000)
	@Inner(false)
	@Operation(summary = "获取全部下级数据", description = "测试用-正式的还是生成json文件cdn把"	)
	@GetMapping("/listByParentCode")
	public R<List<SysArea>> listByParentCode(@Parameter(description = "父code(为空则省份)") String parentCode
			,@Parameter(description = "最大级联-默认/最大3")Integer maxLv) {
		if(maxLv == null || maxLv <= 0 || maxLv > 3)
			maxLv = 3;
		List<SysArea> result = null;
		if(StrUtil.isEmpty(parentCode)){
			result = sysAreaService.getByLv(1);
		}else{
			result = sysAreaService.getByParentCode(parentCode);
		}

		int maxLvFinal = maxLv;
		if(maxLvFinal > 1){
			result.forEach(a->{
				if(a.getLv() < 5) {
					a.setChildren(sysAreaService.getByParentCode(a.getCode()));

					if (maxLvFinal > 2){
						a.getChildren().forEach(aa->{
							if(aa.getLv() < 5) {
								aa.setChildren(sysAreaService.getByParentCode(aa.getCode()));
							}
						});
					}
				}
			});
		}


		return R.ok(result);
	}


	/**
	 * 重新初始化省市区
	 * @return success/false
	 */
	@Inner(false)
	@PutMapping("/initAll")
	/*@SysLog("重新初始化省市区")
	@PreAuthorize("@pms.hasPermission('sys_area_init')")*/
	public R initAll() {
		sysAreaService.initAllByNet(4);
		return R.ok();
	}


}
